package com.dd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.entity.PageResult;
import com.dd.entity.Products;
import com.dd.entity.dto.ProductDTO;
import com.dd.entity.vo.ProductVO;
import com.dd.mapper.ProductsMapper;
import com.dd.service.IProductsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 供应商商品信息表 服务实现类
 * </p>
 *
 * @author dhd
 * @since 2024-12-10 03:50
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements IProductsService {
    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public PageResult<ProductVO> getPage(ProductDTO productDto) {
        Page page = new Page(productDto.getPageNum(), 5);
        QueryWrapper<ProductDTO> queryWrapper = new QueryWrapper<>();

        if (productDto == null) {
            // 如果 productDto 为 null，查询所有记录
            Page<ProductVO> productVOPage = productsMapper.selectProductsPage(page, queryWrapper);
            PageResult pageResult = new PageResult(productVOPage);
            return pageResult;
        }

        if (StringUtils.isNotEmpty(productDto.getProductName())) {
            queryWrapper.like("product_name", productDto.getProductName());
        }
        if (productDto.getSupplierId() != null) {
            queryWrapper.eq("supplier_id", productDto.getSupplierId());
        }
        if (StringUtils.isNotEmpty(productDto.getSupplierName())) {
            queryWrapper.like("supplier_name", productDto.getSupplierName());
        }

        Page<ProductVO> productVOPage = productsMapper.selectProductsPage(page, queryWrapper);
        PageResult pageResult = new PageResult(productVOPage);
        return pageResult;
    }

}
